package com.dkd.day20250616;


public class Solutiontrans {
    public static void main(String[] args) {
        String s1 = "Hello World";
        System.out.println(trans(s1,11));
    }
    public static String trans (String s, int n) {
        // write code here
        // write code here
        if (n==0) return s;
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < s.length(); i++) {
            //大小写转换
            char c = s.charAt(i);
            if(c>='A' && c<='Z'){
                sb.append((char)(c+32));
            }else if(c>='a' && c<='z'){
                sb.append((char)(c-32));
            }else{
                sb.append(c);
            }
        }
        sb=sb.reverse();
        for (int i = 0; i < n; i++){
            int j = i;
            //以空格为界，二次翻转
            while(j < n && sb.charAt(j) != ' ')
                j++;
            String temp = sb.substring(i,j);
            StringBuffer buffer = new StringBuffer(temp);
            temp = buffer.reverse().toString();
            sb.replace(i,j,temp);
            i = j;
        }
        return sb.toString();
    }
}
